package ch11;

import java.util.Comparator;
import java.util.TreeSet;

class _16_ComparatorEx {
	public static void main(String[] args) {
		TreeSet<Integer> set1 = new TreeSet<Integer>();
		TreeSet<Integer> set2 = new TreeSet<Integer>(new Descending());

		int[] score = { 30, 50, 10, 20, 40 };

		for (int i = 0; i < score.length; i++) {
			set1.add(score[i]);
			set2.add(score[i]);
		}

		System.out.println("set1 :" + set1); // set1 :[10, 20, 30, 40, 50]
		System.out.println("set2 :" + set2); // set2 :[50, 40, 30, 20, 10]
	}
}

@SuppressWarnings("unchecked")
class Descending implements Comparator<Object> {
	public int compare(Object o1, Object o2) {
		if (o1 instanceof Comparable && o2 instanceof Comparable) {
			Comparable<Object> c1 = (Comparable<Object>) o1;
			Comparable<Object> c2 = (Comparable<Object>) o2;
			return c1.compareTo(c2) * -1; 
		}
		return -1;
	}
}